package com.dycx.message.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.message.converter.MessageTaskConverter;
import com.dycx.message.dto.MessageTaskDTO;
import com.dycx.message.entity.MessageTaskDO;
import com.dycx.message.mapper.MessageTaskMapper;
import com.dycx.message.service.MessageTaskService;
import com.dycx.message.vo.MessageTaskVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 模版任务表(MessageTask)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-11-14 09:23:58
 */

@Service
@AllArgsConstructor
public class MessageTaskImpl extends ServiceImpl<MessageTaskMapper, MessageTaskDO> implements MessageTaskService {

    private MessageTaskMapper messageTaskMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<MessageTaskVO> getPage(MessageTaskDTO dto) {
        LambdaQueryWrapper<MessageTaskDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getTaskName()), MessageTaskDO::getTaskName, dto.getTaskName());
        wrapper.orderByDesc(MessageTaskDO::getId);

        Page<MessageTaskDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<MessageTaskVO> voPage = MessageTaskConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public MessageTaskVO load(Integer id) {
        MessageTaskDO entity = getById(id);

        return setData(MessageTaskConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public MessageTaskVO setData(MessageTaskVO vo) {
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(MessageTaskDTO dto) {
        MessageTaskDO entity = BeanUtil.toBean(dto, MessageTaskDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(MessageTaskDTO dto) {
        MessageTaskDO entity = BeanUtil.toBean(dto, MessageTaskDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return messageTaskMapper.getMaxSort();
    }
}

